package demo1;

public class MyBinaryTree {
    class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }
    //前序遍历
    public void preOrder(TreeNode root) {
        if (root == null) {return;}
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }
    //中序遍历
    public void inOder (TreeNode root) {
        if (root == null) {return;}
        preOrder(root.left);
        System.out.print(root.val + " ");
        preOrder(root.right);
    }
    //后序遍历
    public void postOrder(TreeNode root) {
        if (root == null) {return;}
        preOrder(root.left);
        preOrder(root.right);
        System.out.print(root.val + " ");
    }
    //获取树中节点的个数
    public int size(TreeNode root) {
        if (root == null) {return 0;}
        return size(root.left) + size(root.right) + 1;
    }
    //获取树中叶子结点的个数
    public int getLeafCount(TreeNode root) {
        if (root == null) {return 0;}
        if (root.left == null && root.right == null) {return 1;}
        return getLeafCount(root.left) + getLeafCount(root.right);
    }
    //层次遍历

    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;
    }
}
